package admindao

import (
	"go.uber.org/zap"
	"strings"
	"wzgames/internal/admin/model"
	"wzgames/internal/gdao"
	"wzgames/pkg/logger"
)

type GameDao gdao.DaoSource

const sqlSysGameCount = "select count(*) from sys_games"

func (dao *GameDao) GetSysGameTotal(name, tag string, grp int32) (int, error) {
	var c int
	var sb strings.Builder
	sb.WriteString(sqlSysGameCount)
	s1, args := dao.genWhere(name, tag, grp)
	sb.WriteString(s1)
	err := dao.Get(&c, sb.String(), args...)
	return c, err
}

func (dao *GameDao) genWhere(name, tag string, grp int32) (string, []interface{}) {
	var sb strings.Builder
	var items []string
	var args []interface{}
	if name != "" || tag != "" {
		sb.WriteString(" where ")
		if name != "" {
			items = append(items, " `name` like CONCAT('%',?,'%') ")
			args = append(args, name)
		}
		if tag != "" {
			items = append(items, " `gtag` like CONCAT('%',?,'%') ")
			args = append(args, tag)
		}
	}
	if grp > 0 {
		items = append(items, " `grp`=? ")
		args = append(args, grp)
	}
	if len(items) > 0 {
		sb.WriteString(strings.Join(items, " and "))
	}
	return sb.String(), args
}

const sqlGetSysGames = "select * from sys_games"

func (dao *GameDao) GetSysGameList(name, tag string, grp int32, offset, pageSize int) ([]*model.SysGame, error) {
	var sb strings.Builder
	sb.WriteString(sqlGetSysGames)
	s1, args := dao.genWhere(name, tag, grp)
	sb.WriteString(s1)
	if pageSize == 0 {
		sb.WriteString(" order by updated desc")
	} else {
		sb.WriteString(" order by updated desc limit ?,?")
		args = append(args, offset, pageSize)
	}
	rows, err := dao.Queryx(sb.String(), args...)
	if err != nil {
		//logger.Error("err == ", zap.String("error", err.Error()))
		return nil, err
	}
	var ret []*model.SysGame
	for rows.Next() {
		rd := &model.SysGame{}
		err = rows.StructScan(rd)
		if err != nil {
			logger.Error("err == ", zap.String("error", err.Error()))
			return nil, err
		}
		ret = append(ret, rd)
	}
	return ret, err
}

const sqlGetSysGameByTag = "select * from sys_games where `gtag`=? limit 1"

func (dao *GameDao) GetSysGameByTag(name string) (*model.SysGame, error) {
	acc := &model.SysGame{}
	err := dao.Get(acc, sqlGetSysGameByTag, name)
	return acc, err
}

const sqlGetSysGameById = "select * from sys_games where `gameid`=? limit 1"

func (dao *GameDao) GetSysGameById(gameId int32) (*model.SysGame, error) {
	acc := &model.SysGame{}
	err := dao.Get(acc, sqlGetSysGameById, gameId)
	return acc, err
}

const sqlCreateSysGame = "insert into sys_games(`gameid`,`name`,`gtag`,`status`,`langs`,`grp`,`created`,`updated`) values(:gameid,:name,:gtag,:status,:langs,:grp,:created,:updated)"

func (dao *GameDao) CreateSysGame(c *model.SysGame) error {
	_, err := dao.NamedExec(sqlCreateSysGame, c)
	return err
}

const sqlUpdateSysGame = "update sys_games set `name`=:name,`gtag`=:gtag,`grp`=:grp,`status`=:status,`updated`=:updated where `gameid`=:gameid"

func (dao *GameDao) UpdateSysGame(c *model.SysGame) (int64, error) {
	result, err := dao.NamedExec(sqlUpdateSysGame, c)
	if err != nil {
		logger.Error("err == ", zap.String("error", err.Error()))
		return 0, err
	}
	return result.RowsAffected()
}

const sqlUpdateSysGameLang = "update sys_games set `langs`=:langs,`updated`=:updated where `gameid`=:gameid"

func (dao *GameDao) UpdateSysGameLang(c *model.SysGame) (int64, error) {
	result, err := dao.NamedExec(sqlUpdateSysGameLang, c)
	if err != nil {
		logger.Error("err == ", zap.String("error", err.Error()))
		return 0, err
	}
	return result.RowsAffected()
}
